<?php
// | Copyright (c) 2019~2020 http://jiunidu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: huanglijun <80792275@qq.com>
// +----------------------------------------------------------------------

namespace other;

use think\cache\driver\Redis;
use \think\facade\Env;
/**
 * 
 * 扩展TP5，Redis类
 * modify by xg 2019-09-20 改为单例
 *
 */
class BaseRedis extends Redis
{

    /**
     * 保存全局实例
     */
    private static $instance = null;

    /**
     * 结构函数
     * @access private
     * @param  array $options 缓存参数
     */
    private function __construct($options = [])
    {
        $redis_options = [
            'type'	    => 'redis',
            'host'	    => Env::get('redis.host','127.0.0.1'),
            'port'      => Env::get('redis.port',6379),
            'password'  => Env::get('redis.password',''),
            'timeout'   => Env::get('redis.timeout'),
            'expire'    => 0, 
            'prefix'    => '',
        ];

        if (!empty($options))  {
            $redis_options = array_merge($redis_options, $options);
        }

        parent::__construct($redis_options);
    }

    /**
     * 防止clone 多个实列
     */
    private function __clone()
    {
 
    }
 
    /**
     * 防止反序列化
     */
    private function __wakeup()
    {

    }

    /**
     * 单例访问统一入口
     * @access public
     * @param  array $options 缓存参数
     */
    public static function getInstance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

}